package com.dwxt.cdms.admin.service;

import com.dwxt.cdms.base.OperateCategory;
import com.dwxt.cdms.dao.MenuMapper;
import com.dwxt.cdms.dao.RoleMapper;
import com.dwxt.cdms.dao.RoleMenuMapper;
import com.dwxt.cdms.entity.*;
import com.dwxt.cdms.entity.dto.RoleDTO;
import com.dwxt.cdms.entity.vo.RoleVO;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.common.util.JsonUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceAdmin {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private HistoryServiceAdmin historyServiceAdmin;

    @Transactional
    public BaseResult createOrUpdateRole(RoleDTO roleDTO) {
        if (roleDTO.getMenuIds() == null) {
            return BaseResult.build(StatusCode.MENU_NOT_EXIST);
        }
        if (roleDTO.getId() == null) {
            Role role = selectByName(roleDTO.getName());
            if (role == null) {
                role = new Role();
                role.setName(roleDTO.getName());
                int i = roleMapper.insertSelective(role);
                Integer roleId = role.getId();
                //批量插入角色菜单记录
                batchInsertRoleMenu(roleDTO.getMenuIds(), roleId);
                //插入操作记录
                historyServiceAdmin.generate("新建角色:" + role.getName(), OperateCategory.INSERT);
                /*//批量插入角色权限记录
                List<Permission> permissions = permissionServiceAdmin.selectByCategoryList(roleDTO.getCategoryList());
                batchInsertRolePermission(roleId, permissions);*/
                return BaseResult.ok();
            } else {
                return BaseResult.build(StatusCode.ROLE_NAME_EXIST);
            }
        } else {
            roleMapper.updateByPrimaryKeySelective(roleDTO);
            //插入操作记录
            historyServiceAdmin.generate("修改角色:" + roleDTO.getId(), OperateCategory.UPDATE);
            //删除原有的roleMenu
            RoleMenuExample roleMenuExample = new RoleMenuExample();
            Integer roleId = roleDTO.getId();
            roleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
            roleMenuMapper.deleteByExample(roleMenuExample);
            //批量插入角色菜单记录
            List<Integer> menuIds = roleDTO.getMenuIds();
            batchInsertRoleMenu(menuIds, roleId);

            return BaseResult.ok();
        }
    }


    private void batchInsertRoleMenu(List<Integer> id, Integer roleId) {
        List<RoleMenu> roleMenus = id.stream().map(menuId -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            return roleMenu;
        }).collect(Collectors.toList());
        roleMenuMapper.batchInsert(roleMenus);
    }

    public Role selectByName(String name) {
        RoleExample example = new RoleExample();
        example.createCriteria().andNameEqualTo(name);
        List<Role> roles = roleMapper.selectByExample(example);
        return roles.isEmpty() ? null : roles.get(0);
    }

    @Transactional
    public int deleteByPrimaryKey(Integer id) {

        int i = roleMapper.deleteByPrimaryKey(id);
        //插入操作记录
        historyServiceAdmin.generate("删除角色:" + id, OperateCategory.DELETE);
        return i;
    }


    public BaseResult selectAllRoleVo() {
        List<Role> roleVOS = new ArrayList<>();
        RoleExample example = new RoleExample();
        List<Role> roleList = roleMapper.selectByExample(example);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);
        for (Role role : roleList) {
            RoleVO roleVo = JsonUtils.jsonToPojo(JsonUtils.objectToJson(role), RoleVO.class);
            Integer roleId = roleVo.getId();

            RoleMenuExample roleMenuExample = new RoleMenuExample();
            roleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
            List<RoleMenu> roleMenus = roleMenuMapper.selectByExample(roleMenuExample);
            List<Integer> collect = roleMenus.stream().map(roleMenu -> {
                return roleMenu.getMenuId();
            }).collect(Collectors.toList());
            List<Menu> menus = new ArrayList<>();
            if (collect.size() != 0) {
                MenuExample menuExample = new MenuExample();
                menuExample.createCriteria().andIdIn(collect);
                menus = menuMapper.selectByExample(menuExample);
            }
            roleVo.setMenus(menus);
            roleVOS.add(roleVo);
        }
        pageInfo.setList(roleVOS);
        return BaseResult.ok(pageInfo);
    }


    public List<Role> selectAllRole() {
        RoleExample example = new RoleExample();
        List<Role> roles = roleMapper.selectByExample(example);
        roles = roles.stream().filter(role -> role.getId() != 1).collect(Collectors.toList());
        return roles;
    }

}
